﻿Imports System.Xml

Namespace Objects

    Public Class DocumentCel

        Public Enum BorderType
            None
            Full
            Top
            Bottom
        End Enum

        Private _font As Font
        Private _foregroundColor As Brush = Brushes.Black
        Private _backgroundColor As Color = Color.Transparent
        Private _border As BorderType = BorderType.None
        Private _afbeelding As Image
        Private _id As String
        Private _tekst As String
        Private _alignment As StringAlignment = StringAlignment.Near
        Private _widthPercent As Single

#Region "Public property's"

        Public Property Font() As Font
            Get
                Return _font
            End Get
            Set(ByVal value As Font)
                _font = value
            End Set
        End Property

        Public Property ForegroundColor() As Brush
            Get
                Return _foregroundColor
            End Get
            Set(ByVal value As Brush)
                _foregroundColor = value
            End Set
        End Property

        Public Property BackgroundColor() As Color
            Get
                Return _backgroundColor
            End Get
            Set(ByVal value As Color)
                _backgroundColor = value
            End Set
        End Property

        Property Border() As BorderType
            Get
                Return _border
            End Get
            Set(ByVal value As BorderType)
                _border = value
            End Set
        End Property

        Public Property Afbeelding() As Image
            Get
                Return _afbeelding
            End Get
            Set(ByVal value As Image)
                _afbeelding = value
            End Set
        End Property

        Public Property ID() As String
            Get
                Return _id
            End Get
            Set(ByVal value As String)
                _id = value
            End Set
        End Property

        Public Property Tekst() As String
            Get
                Return _tekst
            End Get
            Set(ByVal value As String)
                _tekst = value
            End Set
        End Property

        Public Property Alignment() As StringAlignment
            Get
                Return _alignment
            End Get
            Set(ByVal value As StringAlignment)
                _alignment = value
            End Set
        End Property

        ''' <summary>
        ''' Breedte van de cel in %
        ''' Indien de waarde -1 is, dan wordt de breedte bepaald door de andere cellen in de rij
        ''' </summary>
        Public Property WidthPercent() As Single
            Get
                Return _widthPercent
            End Get
            Set(ByVal value As Single)
                _widthPercent = value
            End Set
        End Property

#End Region

#Region "Constructor/initialisatie"

        Public Sub New(ByVal celNode As XmlNode, ByVal defaultFont As Font)
            ' Font
            GetFont(celNode, defaultFont)
            ' Width
            Dim widthAttribute As XmlAttribute = celNode.Attributes("width")
            _widthPercent = -1
            If (widthAttribute IsNot Nothing) Then
                _widthPercent = Convert.ToInt32(widthAttribute.Value.Replace("%", ""))
            End If
            ' Achtergrondkleur
            Dim backcolorAttribute As XmlAttribute = celNode.Attributes("backcolor")
            If (backcolorAttribute IsNot Nothing) Then
                ' kleur uit attribuut halen
                _backgroundColor = Color.FromName(backcolorAttribute.Value)
            End If
            ' Randen
            Dim borderAttribute As XmlAttribute = celNode.Attributes("border")
            If (borderAttribute IsNot Nothing) Then
                Select Case borderAttribute.Value
                    Case "full"
                        _border = BorderType.Full
                    Case "bottom"
                        _border = BorderType.Bottom
                    Case "top"
                        _border = BorderType.Top
                End Select
            End If
            ' Afbeelding
            Dim afbeeldingAttribute As XmlAttribute = celNode.Attributes("afbeelding")
            If (afbeeldingAttribute IsNot Nothing) Then
                _afbeelding = DirectCast(My.Resources.Images.ResourceManager.GetObject(afbeeldingAttribute.Value), Image)
            End If
            ' Tekst
            _tekst = ""
            Dim tekstAttribute As XmlAttribute = celNode.Attributes("tekst")
            If (tekstAttribute IsNot Nothing) Then
                _tekst = tekstAttribute.Value
            End If
            ' ID
            Dim idAttribute As XmlAttribute = celNode.Attributes("id")
            If (idAttribute IsNot Nothing) Then
                _id = idAttribute.Value
            End If
            ' Alignment 
            Dim textalignAttribute As XmlAttribute = celNode.Attributes("textalign")
            If (textalignAttribute IsNot Nothing) Then
                Select Case textalignAttribute.Value
                    Case "center"
                        _alignment = StringAlignment.Center
                    Case "right"
                        _alignment = StringAlignment.Far
                    Case "left"
                        _alignment = StringAlignment.Near
                End Select
            End If
        End Sub

#End Region

#Region "Public methods"

        Public Sub Print(ByVal left As Integer, ByVal top As Integer, _
                         ByVal breedte As Integer, ByVal hoogte As Integer, ByVal gdiPage As Graphics)
            Dim bounds As New Rectangle(left, top, breedte, hoogte)
            ' Achtergrondkleur
            If (_backgroundColor <> Color.Transparent) Then
                ' kleur uit attribuut halen
                Dim brush As New SolidBrush(_backgroundColor)
                gdiPage.FillRectangle(brush, bounds)
            End If
            ' Randen
            Select Case _border
                Case BorderType.Full
                    gdiPage.DrawRectangle(Pens.Black, bounds)
                Case BorderType.Bottom
                    gdiPage.DrawLine(Pens.Black, left, top + hoogte, left + breedte, top + hoogte)
                Case BorderType.Top
                    gdiPage.DrawLine(Pens.Black, left, top, left + breedte, top)
            End Select
            ' Afbeelding
            If (_afbeelding IsNot Nothing) Then
                Dim imageHoogte As Integer = _afbeelding.Height * breedte \ _afbeelding.Width
                gdiPage.DrawImage(_afbeelding, left, top, breedte, imageHoogte)
                Return  ' tekst wordt hier genegeerd (als er al tekst zou zijn)
            End If
            ' Alignment en afdrukken
            Dim format As New StringFormat()
            format.Alignment = _alignment
            gdiPage.DrawString(Tekst, Font, Me._foregroundColor, bounds, format)
        End Sub

        Public Function BerekenTekstHoogte(ByVal breedte As Integer, ByVal gdiPage As Graphics) As Integer
            Dim hoogte As Integer = Convert.ToInt32(gdiPage.MeasureString(_tekst, _font, breedte).Height)
            If (hoogte < font.Height) Then hoogte = font.Height
            Return hoogte
        End Function

#End Region

#Region "Private methods"

        Private Sub GetFont(ByVal celNode As XmlNode, ByVal defaultFont As Font)
            _font = defaultFont ' default font
            Dim fontAttribuut As XmlAttribute = celNode.Attributes("font")
            Dim fontsizeAttribuut As XmlAttribute = celNode.Attributes("fontsize")
            Dim fontweightAttribute As XmlAttribute = celNode.Attributes("fontweight")
            If (fontAttribuut IsNot Nothing OrElse fontsizeAttribuut IsNot Nothing OrElse fontweightAttribute IsNot Nothing) Then
                Dim fontName As String = defaultFont.Name
                If (fontAttribuut IsNot Nothing) Then
                    fontName = fontAttribuut.Value
                End If
                Dim fontSize As Single = defaultFont.Size
                If (fontsizeAttribuut IsNot Nothing) Then
                    fontSize = Convert.ToSingle(fontsizeAttribuut.Value)
                End If
                If (fontweightAttribute IsNot Nothing AndAlso fontweightAttribute.Value = "bold") Then
                    _font = New Font(fontName, fontSize, FontStyle.Bold)
                Else
                    _font = New Font(fontName, fontSize)
                End If
            End If
        End Sub

#End Region

    End Class

End Namespace

